/* 
 * File:   Map.cpp
 * Author: chb
 * 
 * Created on 5. September 2009, 11:32
 */

#include "Map.h"

using namespace std;

Map::Map() {
}

Map::Map(const Map& orig) {
}

Map::~Map() {
    for(vector<MapSite*>::iterator i = _mapSites.begin(); i != _mapSites.end(); ++i) {
        delete *i;
    }
}

void Map::addMapSite(MapSite* m) {
    _mapSites.push_back(m);
}

MapSite* Map::getMapSite(unsigned int x, unsigned int y) {
    for(vector<MapSite*>::const_iterator i = _mapSites.begin(); i != _mapSites.end(); ++i) {
        if((*i)->getPosition()->x == x && (*i)->getPosition()->y == y)
            return *i;
    }
    return 0;
}

void Map::paint(View* v) {
    for(vector<MapSite*>::const_iterator i = _mapSites.begin(); i != _mapSites.end(); ++i) {
        (*i)->paint(v);
    }
}

void Map::createMapSite(unsigned int x, unsigned int y, MapSite::MS_TYPE type){

    MapSite* m = MapSiteCatalog::instance()->getNewMapSite(x, y, type);
    
    MapSite* tBottom = getMapSite(x, y+1);
    MapSite* tTop = getMapSite(x, y-1);
    MapSite* tLeft = getMapSite(x-1, y);
    MapSite* tRight = getMapSite(x+1, y);

    m->setBottomSide(tBottom);
    m->setTopSide(tTop);
    m->setLeftSide(tLeft);
    m->setRightSide(tRight);

    if (tBottom != 0)
        tBottom->setTopSide(m);
    if (tTop != 0)
        tTop->setBottomSide(m);
    if (tLeft != 0)
        tLeft->setRightSide(m);
    if (tRight != 0)
        tRight->setLeftSide(m);

    this->addMapSite(m);
}